package com.zr.controller;
/**
 * 修改密码的servlet层
 */
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.User;
import com.zr.service.UserService;

public class UpdatePswController extends HttpServlet {
	UserService us=new UserService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 String oldpsw = req.getParameter("oldpsw");
	     String newpsw = req.getParameter("newpsw2");
	     HttpSession session = req.getSession();
	     User currentUser = (User) session.getAttribute("currentUser");
	     if(oldpsw.equals(currentUser.getUpsw())){
	    	 //调用提供修改密码的服务
	    	 boolean  flag =us.updataPsw(newpsw, currentUser.getUid());
	    	 if(flag){
	    		 req.setAttribute("Msg", "密码修改成功");
		    	 req.getRequestDispatcher("updatepsw.jsp").forward(req, resp);
	    	 }
	    	 else{
	    		 req.setAttribute("Msg", "对不起,新密码修改错误");
		    	 req.getRequestDispatcher("updatepsw.jsp").forward(req, resp);
	    	 }
	    	 
	     }else{
	    	 req.setAttribute("Msg", "对不起你的原密码错误");
	    	 req.getRequestDispatcher("updatepsw.jsp").forward(req, resp);
	     }
	}
	}
